const express = require('express');
var elasticsearch = require('elasticsearch');
const fs = require('fs');
var cors = require('cors')
const app = express();
var  bodyParser = require( 'body-parser' );
 
const PORT = 5000;
const verify = require('./verify');
const searctTerm = require('./search_term');
const data_Visible = require('./data_Visible');
const data_Export = require('./data_Export');

app.use(cors())
app.use(express.json())
app.use(bodyParser.json());
const client = new elasticsearch.Client({
    host: '127.0.0.1:9200',
    log: 'error'
});

client.ping({ requestTimeout: 30000 }, function(error) {
    if (error) {
        console.error('elasticsearch cluster is down!');
    } else {
        console.log('Everything is ok');
    }
});
 
 
const bulkIndex = function bulkIndex(index, type, data) {
    let bulkBody = [];
 
    data.forEach(item => {
        bulkBody.push({
            index: {
                _index: index,
                _type: type,
                _id: item.id
            }
        });
 
        bulkBody.push(item);
    });
 
    client.bulk({body: bulkBody})
        .then(response => {
            let errorCount = 0;
            response.items.forEach(item => {
                if (item.index && item.index.error) {
                    console.log(++errorCount, item.index.error);
                }
            });
            console.log(
                `Successfully indexed ${data.length - errorCount}
         out of ${data.length} items`
            );
        })
        .catch(console.err);
};
 
async function indexData() {
    const twittersRaw = await fs.readFileSync('./data.json');
    const twitters = JSON.parse(twittersRaw);
    console.log(`${twitters.length} items parsed from data file`);
    bulkIndex('data', '_doc', twitters);
};
 
//indexData();
verify();

app.post('/search', async (req, res) => {
    let visibleData = data_Visible(req.body);  
    console.log(req.body);
    let response = await searctTerm(req.body);
    res.send(response);
})

app.post('/export', async (req,res)=>{
    let response = await data_Export()
    res.send(response)
})
app.listen(PORT, function() {
    console.log('Server is running on PORT:',PORT);
});